// 猜你喜欢
sendAxios('https://pcapi-xiaotuxian-front.itheima.net/home/goods/guessLike').then(data=>{
    console.log(data)
    const items = data.result.items;
    const couple = document.querySelectorAll('.iot_page1 .iot_couple');
    for(let i = 0 ; i < 5 ; i++){
        let img = couple[i].querySelectorAll('img')
        let a = couple[i].querySelectorAll('a')
        for(let j = 0 ; j < 2 ; j++){
        img[j].src = items[2*i+j].picture;
        img[j].id = items[2*i+j].id;
        a[j].addEventListener('click', function(event) { 
            const id = this.querySelector('img').id;
            event.preventDefault(); 
            console.log(id);
            sendMessage(id,0);
            this.style.opacity = '.7'
            setTimeout(() => {
            this.style.opacity = '1'
            }, 500);
        }); 
       } 
    }
    for(let i = 0 ; i < 5 ; i++){
        let p = couple[i].querySelectorAll('p')
        for(let j = 0 ; j < 4 ; j++){
        if (j === 0){
            p[j].innerHTML = items[2*i].name;
        }else if(j === 1){
            p[j].innerHTML = '￥'+items[2*i].price;
        }else if(j === 2){
            p[j].innerHTML = items[2*i+1].name;
        }else if(j === 3){
            p[j].innerHTML = '￥'+items[2*i+1].price;
        }
       } 
    }
})

//加载更多猜你喜欢 
const screenHeight = this.screen.availHeight;
const screenWidth = this.screen.availWidth;
document.addEventListener('DOMContentLoaded', function() { 
    const bottom = document.querySelector('.iot_loading')
    let bottomNav = 1.3178*screenWidth/10;
    let i = 2;
    let axiosData = null;
    window.addEventListener("scroll", function(){
        if(document.documentElement.scrollTop > document.body.scrollHeight - screenHeight + bottomNav){
            axiosData = sendingAxios(`https://pcapi-xiaotuxian-front.itheima.net/home/goods/guessLike?page=${i}&pageSize=10`);
            if (axiosData){
                axiosData.then(data=>{
                    const items = data.result.items;
                    const page = document.createElement('div');
                    page.className = `iot_page${i}`;
                    page.innerHTML = '<div class="iot_couple"><a href=""><img src="" alt=""><p></p><p></p></a><a href=""><img src="" alt=""><p></p><p></p></a></div><div class="iot_couple"><a href=""><img src="" alt=""><p></p><p></p></a><a href=""><img src="" alt=""><p></p><p></p></a></div><div class="iot_couple"><a href=""><img src="" alt=""><p></p><p></p></a><a href=""><img src="" alt=""><p></p><p></p></a></div><div class="iot_couple"><a href=""><img src="" alt=""><p></p><p></p></a><a href=""><img src="" alt=""><p></p><p></p></a></div><div class="iot_couple"><a href=""><img src="" alt=""><p></p><p></p></a><a href=""><img src="" alt=""><p></p><p></p></a></div>'
                    const pages = document.querySelector('.iot_pages')
                    pages.appendChild(page);
                    const couple = page.querySelectorAll('.iot_couple');
                    for(let i = 0 ; i < 5 ; i++){
                        let img = couple[i].querySelectorAll('img')
                        let a = couple[i].querySelectorAll('a')
                        for(let j = 0 ; j < 2 ; j++){
                        img[j].src = items[2*i+j].picture;
                        img[j].id = items[2*i+j].id;
                        a[j].addEventListener('click', function(event) { 
                            const id = this.querySelector('img').id;
                            event.preventDefault(); 
                            console.log(id);
                            sendMessage(id,0);
                        }); 
                       } 
                    }
                    for(let i = 0 ; i < 5 ; i++){
                        let p = couple[i].querySelectorAll('p')
                        for(let j = 0 ; j < 4 ; j++){
                        if (j === 0){
                            p[j].innerHTML = items[2*i].name;
                        }else if(j === 1){
                            p[j].innerHTML = '￥'+items[2*i].price;
                        }else if(j === 2){
                            p[j].innerHTML = items[2*i+1].name;
                        }else if(j === 3){
                            p[j].innerHTML = '￥'+items[2*i+1].price;
                        }
                       } 
                    }
                    i++;
                })
            }
            
        }
    })
}); 

